package day15.test06;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Xiao
 * @date 2021/8/9
 */
public class Server06 {
    /**
     * 6. 使用TCP完成文件上传。
     */
    public static void main(String[] args) throws IOException {
        //创建ServerSocket对象，建立连接
        ServerSocket ss = new ServerSocket(12121);
        //调用accept()方法，监视客户端
        Socket so = ss.accept();
        //从客户端读入文件到服务器，再从服务器写出文件到本地文件夹
        BufferedInputStream bis = new BufferedInputStream(so.getInputStream());
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
                "homework\\src\\day15\\test06\\aaCopy.txt"));
        int b;
        while ((b = bis.read()) != -1) {
            bos.write(b);
        }
        System.out.println("服务器上传文件。。。");
        bos.close();
        so.shutdownInput();
        //将反馈信息写出给客户端
        OutputStream os = so.getOutputStream();
        os.write("文件上传完成。。。\r\nOver。。。".getBytes());
        so.close();
        ss.close();
    }
}
